package cn.regexp.web.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.regexp.annotation.RequiredLogin;
import cn.regexp.domain.ShopCollect;
import cn.regexp.service.ICollectService;
import cn.regexp.utils.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("collects")
public class CollectController {
    @Autowired
    private ICollectService collectService;

    @GetMapping("/collect")
    public JsonResult<String> collect(Long goodsId, Long userId) {
        boolean result = collectService.collect(goodsId, userId);
        return  result ? JsonResult.success() : JsonResult.error(0, "收藏失败", null);
    }

    @GetMapping("/cancelCollect")
    @RequiredLogin
    public JsonResult<String> cancelCollect(Long goodsId, Long userId) {
        boolean result = collectService.cancelCollect(goodsId, userId);
        return  result ? JsonResult.success() : JsonResult.error(0, "取消失败", null);
    }

    @GetMapping("/isCollect")
    @RequiredLogin
    public JsonResult<Boolean> isCollect(Long goodsId, Long userId) {
        boolean result = collectService.isCollect(goodsId, userId);
        return  JsonResult.success(result);
    }

    @GetMapping("/queryCollectGoodsByUserId")
    @RequiredLogin
    public JsonResult<List<ShopCollect>> queryCollectGoodsByUserId(Long userId) {
        if (ObjectUtil.isEmpty(userId)) {
            return JsonResult.error(0, "请先登录！", null);
        }
        List<ShopCollect> collects = collectService.queryCollectGoodsByUserId(userId);
        return JsonResult.success(collects);
    }
}
